#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define rep(Ii,Jj,Kk) for(int Ii=(Jj),Ii##_=(Kk);Ii<=Ii##_;Ii++)
#define per(Ii,Jj,Kk) for(int Ii=(Jj),Ii##_=(Kk);Ii>=Ii##_;Ii--)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned uint;
typedef long double db;
#define fir first
#define sec second
#define siz(Aa) ((int)(Aa).size())
#define all(Aa) (Aa).begin(),(Aa).end()
#define ckmx(Aa,Bb) (Aa=max(Aa,Bb))
#define ckmn(Aa,Bb) (Aa=min(Aa,Bb))
#define Fi(s) freopen(s,"r",stdin)
#define Fo(s) freopen(s,"w",stdout)
#define Fre(s) Fi(s".in"),Fo(s".out")
const int N=8010,M=60010;
int n,m,a[N],b[N],w[N];
bool v[M];
int o(int x,int y){
	return abs(a[x]-a[y])+abs(b[x]-b[y]);
}
void work(){
	cin>>n>>m;
	rep(i,1,n) cin>>a[i]>>b[i];
	rep(i,1,n) cin>>w[i];
	ll cnt=0;
	rep(i,1,n) rep(j,i+1,n) rep(k,j+1,n){
		int c[3]={o(i,j),o(j,k),o(k,i)};
		sort(c,c+3);
		if(v[c[1]]) cnt+=w[i]+w[j]+w[k];
	}
	cout<<cnt<<"\n";
}
bool vis[M];
vector<int> p;
void init(){
	rep(i,2,M-1){
		if(!vis[i]){
			p.emplace_back(i);
		}
		for(int j:p){
			if(i*j>=M) break;
			vis[i*j]=true;
			if(i%j==0) break;
		}
	}
	rep(i,2,M-1) if(!vis[i] && (i%20==3 || i<=10)) v[i]=true;
}
signed main(){ios::sync_with_stdio(false),cin.tie(nullptr);
	Fre("chess");
	init();
	int T;cin>>T;
	while(T--)work();
return 0;}
/*
*/